from odoo import fields, models, api, _


class WorkflowCheckWizard(models.TransientModel):
    _inherit = "workflow.check.wizard"

    def approval_pass(self):
        rec = super(WorkflowCheckWizard, self).approval_pass()
        workflow = self.sb_workflow_item_id.sb_workflow_id
        if workflow.purchase_order_id and workflow.state == 'approve' and workflow.purchase_order_id.subcontracting:
            purchase = workflow.purchase_order_id
            order_line_ids = [(0, 0, {'purchase_line_id': l.id}) for l in purchase.order_line]
            self.env['stock.subcon.outside'].sudo().create({
                'purchase_id': purchase.id,
                'line_ids': order_line_ids,
            })
        return rec
